package com.itheima.accountingdb.service.impl;

import com.itheima.accountingdb.mapper.TransactionMapper;
import com.itheima.accountingdb.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author panweiwei
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private TransactionMapper transactionMapper;

    @Override
    public Map<String, Object> getSummaryReport(Long userId, String startDate, String endDate, Integer categoryId) {
        return transactionMapper.getSummaryReport(userId, startDate, endDate, categoryId);
    }

    @Override
    public Map<String, Object> getChartData(String startDate, String endDate, String type) {
        List<Map<String, Object>> rawData = transactionMapper.getChartData(startDate, endDate, type);

        List<String> labels = new ArrayList<>();
        List<BigDecimal> incomeData = new ArrayList<>();
        List<BigDecimal> expenseData = new ArrayList<>();

        for (Map<String, Object> entry : rawData) {
            labels.add((String) entry.get("period"));
            incomeData.add((BigDecimal) entry.get("total_income"));
            expenseData.add((BigDecimal) entry.get("total_expense"));
        }

        Map<String, Object> result = new HashMap<>();
        result.put("labels", labels);
        result.put("incomeData", incomeData);
        result.put("expenseData", expenseData);
        return result;
    }
}
